﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FinalProject.Services.Interfaces;
using FinalProject.Data.EntityRepositories.Interfaces;
using FinalProject.Data.DBInteractions.Interfaces;
using FinalProject.Data.Search;
using FinalProject.Entities;

namespace FinalProject.Services.Services
{
    public class UserRoleService : IUserRoleService
    {
        private readonly IUserRoleRepository _repository;
        private readonly IUnitOfWork _unitOfWork;
        public UserRoleService(IUserRoleRepository repository, IUnitOfWork unitOfWork)
        {
            this._repository = repository;
            this._unitOfWork = unitOfWork;
        }

        #region Members

        public PagedListResult<UserRole> GetAll()
        {
            var query = new SearchQuery<UserRole>();
            var entities = _repository.Search(query);
            return entities;
        }

        public Entities.UserRole GetById(int id)
        {
            var entity = _repository.GetById(id);
            return entity;
        }

        public void Create(Entities.UserRole entity)
        {
            _repository.Add(entity);
            _unitOfWork.Commit();
        }

        public void Update(Entities.UserRole entity)
        {
            _repository.Update(entity);
            _unitOfWork.Commit();
        }

        public void Delete(int id)
        {
            var entity = _repository.GetById(id);
            _repository.Delete(entity);
            _unitOfWork.Commit();
        }

        public void Save()
        {
            _unitOfWork.Commit();
        }

        #endregion
        public List<UserRole> GetAllRolesWithId(List<UserRole> newRoles)
        {
            var entities = new List<UserRole>();
            foreach (var role in newRoles)
            {
                entities.Add(_repository.GetById(role.Id));
            }
            return entities;
        }
    }
}
